package com.yonyou.crm.sprc.oppstage.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.bpub.refervalue.entity.ReferDataVO;
import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.enumeration.service.EnumBizServiceImpl;
import com.yonyou.crm.common.field.handler.FieldRelationHandler;
import com.yonyou.crm.common.field.relation.EnumFieldRelation;
import com.yonyou.crm.common.field.relation.FieldRelation;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.sprc.oppstage.entity.OppstageVO;
import com.yonyou.iuap.context.InvocationInfoProxy;

@Service
public class OppstageBizServiceImpl extends BaseBizServiceImpl<OppstageVO> {

	@Autowired
	private EnumBizServiceImpl enumService;

	public List<OppstageVO> getOppstageList(Map<String, Object> paraMap) {

		if (paraMap == null) {
			paraMap = new HashMap<String, Object>();
		}

		paraMap.put("tenantId", new Long(InvocationInfoProxy.getTenantid()));

		List<OppstageVO> voList = dao.selectVoList(OppstageVO.class, "listOppstage", paraMap);

		trans(voList);

		return voList;
	}

	@Override
	protected void beforeSaveVO(OppstageVO vo) {

		super.beforeSaveVO(vo);
		vo.setIsPreseted(2);//非预置
		vo.setEnableState(1);//默认启用
	}

	@Override
	public OppstageVO saveVO(OppstageVO vo) {

		vo = super.saveVO(vo);

		trans(Arrays.asList(vo));

		return vo;
	}

	public OppstageVO getDetail(Long id) {
		OppstageVO vo = this.getVOById(OppstageVO.class, id);
		trans(Arrays.asList(vo));
		return vo;
	}


	public void batchUpdateEnableState(String[] ids, Integer enableState) {

		Map<String, Object> enableParaMap = new HashMap<String, Object>();
		enableParaMap.put("ids", ids);
		enableParaMap.put("enableState", enableState);
		enableParaMap.put("enableUserId", new Long(InvocationInfoProxy.getParameter("id")));
		enableParaMap.put("enableTime", new Date());

		dao.updateSql(OppstageVO.class, "batchUpdateEnableState", enableParaMap);

	}

	private void trans(List<OppstageVO> voList) {

		if (voList != null && voList.size() > 0) {

			List<FieldRelation<OppstageVO>> relations = new ArrayList<FieldRelation<OppstageVO>>();
			relations.add(new EnumFieldRelation<OppstageVO>(OppstageVO.class, "isPreseted", "isPresetedName", enumService, "whether"));
			relations.add(new EnumFieldRelation<OppstageVO>(OppstageVO.class, "enableState", "enableStateName", enumService, "enable_state"));

			new FieldRelationHandler<OppstageVO>().handler(voList, relations);
		}

	}

	/*
	 * 获取参照档案列表
	 */
	public List<ReferDataVO> getRefervalues(Page<OppstageVO> page, Map<String, Object> paraMap) {
		// TODO 加权限
		Page<OppstageVO> pageVO = dao.pageSelect(OppstageVO.class, page, "selectRefervalues", "countRefervalues", paraMap);
		List<OppstageVO> voList = pageVO.getData();

		List<ReferDataVO> rsList = new ArrayList<ReferDataVO>();
		for (OppstageVO vo : voList) {
			ReferDataVO referDataVO = new ReferDataVO();
			referDataVO.setId(vo.getId().toString());
			referDataVO.setValue(vo.getName());

			rsList.add(referDataVO);
		}
		return rsList;
	}
	
	/*
	 * 获取参照档案列表
	 */
	public List<OppstageVO> getRefervaluesForApp(Page<OppstageVO> page, Map<String, Object> paraMap) {
		// TODO 加权限
		Page<OppstageVO> pageVO = dao.pageSelect(OppstageVO.class, page, "selectRefervalues", "countRefervalues", paraMap);
		List<OppstageVO> voList = pageVO.getData();
		return voList;
	}

	public Object selectFieldsByIds(Object[] ids) {
		Map<String, Object> para = new HashMap<String, Object>();
		para.put("ids", ids);
		return dao.selectList(OppstageVO.class, "selectFieldsByIds", para);
	}

	/**
	 * 根据流程获取阶段
	 * @param biztype
	 * @return
	 */
    public List<OppstageVO> getStageByBiztype(String biztype) {
    	Map<String,Object> map = new HashMap<String,Object>();
    	map.put("biztype",biztype);
		List<OppstageVO> voList = dao.selectVoList(OppstageVO.class, "getStageByFlow", map);
		return voList;
    }
    
    /**
	 * 获取当前公司的所有阶段
	 * @return
	 */
    public List<OppstageVO> getStageByTenant() {
    	Map<String,Object> map = new HashMap<String,Object>();
    	map.put("tenantId", new Long(InvocationInfoProxy.getTenantid()));
		List<OppstageVO> voList = dao.selectVoList(OppstageVO.class, "getStageByTenant", map);
		return voList;
    }
    
}
